package com.cms.controller;

import com.cms.entity.User;
import com.cms.service.UserService;
import com.cms.vo.Result;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping(value = "/save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(User user) {
        Integer save = userService.save(user);

        if (save != 1) {
            return new Gson().toJson(Result.error(501,"保存失败"));
        }
        return new Gson().toJson(Result.success());
    }


    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(Integer id) {
        Integer delete = userService.delete(id);
        if (delete != 1) {
            return new Gson().toJson(Result.error(501,"删除失败"));
        }
        return new Gson().toJson(Result.success());
    }


    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list() {
        return new Gson().toJson(Result.success(userService.list()));
    }


    @GetMapping("/savePage")
    public String savePage() {
        return "saveUser";
    }

    @GetMapping("/listPage")
    public String listPage() {
        return "listUser";
    }

    @GetMapping("/passPage")
    public String passPage() {
        return "pass";
    }
}
